package com.dashu.device.service;

import com.alibaba.nacos.common.util.UuidUtils;
import com.dashu.base.enums.AtStatus;
import com.dashu.base.enums.device.*;
import com.dashu.base.utils.OssClienUtils;
import com.dashu.base.utils.date.TimeUtils;
import com.dashu.base.vo.DeviceOpenDoorReqVO;
import com.dashu.device.dto.DeviceDTO;
import com.dashu.device.dto.attendance.AttendanceRuleDTO;
import com.dashu.device.dto.attendance.AttendanceTimeDTO;
import com.dashu.device.model.AttendanceDate;
import com.dashu.device.model.CompanyPersonal;
import com.dashu.device.model.CompanyTeacher;
import com.dashu.device.model.Device;
import com.dashu.device.modelat.Distinguish;
import com.dashu.device.service.HF.HFInsideExternalService;
import com.dashu.device.service.HF.HFnsideInsideService;
import com.dashu.device.service.JL.JLInsideExternalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Service
public class AttendanceBaseServer extends DataService {

    private static AttendanceBaseServer attendanceBaseServer; // 2. 定义本类的静态对象

    @PostConstruct // 3. 添加 @PostConstruct 注解的方法
    public void init() {
        attendanceBaseServer = this;
    }

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private AtService atService;
    @Autowired
    private AttendanceRuleService attendanceRuleService;
    @Autowired
    private AttendanceTimeService attendanceTimeService;
    @Autowired
    private CompanyTeacherService companyTeacherService;
    @Autowired
    private CompanyPersonalService companyPersonalService;
    @Autowired
    private JLInsideExternalService jlInsideExternalService;
    @Autowired
    private HFInsideExternalService hfInsideExternalService;


    public void isAttendance(Distinguish distinguish) throws UnsupportedEncodingException {
        DeviceDTO deviceDTO = attendanceBaseServer.deviceService.findByDeviceUUId(distinguish.getUuid());
        if (deviceDTO != null) {

            if (deviceDTO.getModuleType().equals(ModuleType.SY.name())) {
                distinguish.setInAndOut(deviceDTO.getInAndOut().equals(ParkVdcType.IN.name()) ? ParkVdcType.IN.name() : ParkVdcType.OUT.name());
                if (Iden.TEACHER.name().equals(distinguish.getIden())) {
                    CompanyTeacher companyTeacher = attendanceBaseServer.companyTeacherService.findTeacher(distinguish.getCId());
//            companyTeacher.getTeacherName();
                    distinguish.setCompanyId(companyTeacher.getCompanyId());
                    distinguish.setTeacherType(companyTeacher.getTeacherType().name());
                    distinguish.setIcon(companyTeacher.getIcon());
                    distinguish.setTeacherPhone(companyTeacher.getTeacherPhone());
                    distinguish.setAttendanceId(companyTeacher.getAttendanceId());
                } else if (Iden.STUDENT.name().equals(distinguish.getIden())) {
                    CompanyPersonal companyPersonal = attendanceBaseServer.companyPersonalService.findPersonal(distinguish.getCId());
                    distinguish.setNum(companyPersonal.getNum());
                    distinguish.setPersonalType(companyPersonal.getPersonalType().name());
                    distinguish.setCompanyId(companyPersonal.getCompanyId());
                    distinguish.setDepartmentId(companyPersonal.getDepartmentId());
                    distinguish.setClsId(companyPersonal.getClsId());
                    distinguish.setAttendanceId(companyPersonal.getAttendanceId());
                    distinguish.setIcon(companyPersonal.getIcon());
                    distinguish.setAttendanceId(companyPersonal.getAttendanceId());
                }
                AttendanceRuleDTO attendanceRuleDTO = attendanceBaseServer.attendanceRuleService.findByAttendancekRuleId(distinguish.getAttendanceId());
                if (attendanceRuleDTO != null) {
                    List<AttendanceDate> attendanceDateDTOS = attendanceRuleDTO.getAttendancekDate();
                    long captureTime1 = 0;
                    if (deviceDTO.getDeviceType().equals(DeviceType.JL_CONTROL.name())) {
                        captureTime1 = distinguish.getCaptureTime();
                    } else if (deviceDTO.getDeviceType().equals(DeviceType.HF_CONTROL.name())) {
                        captureTime1 = distinguish.getCaptureTime() * 1000;
                    }
//            Distinguish netDis = (Distinguish) atService.findByid(Distinguish.class, distinguish.getCId());
                    final String[] status = {AtStatus.NORMAL.name()};
                    final long[] num = {0};
                    long finalCaptureTime = captureTime1;
                    attendanceDateDTOS.stream().forEach(ad -> {
                        AttendanceTimeDTO attendanceTimeDTO = attendanceBaseServer.attendanceTimeService.findAttendancekGroup(ad.getTimeId());
                        long startDate1 = TimeUtils.getNowDateAnTime(attendanceTimeDTO.getStartDate1());
                        long startDate2 = TimeUtils.getNowDateAnTime(attendanceTimeDTO.getStartDate2());
                        long startDate3 = TimeUtils.getNowDateAnTime(attendanceTimeDTO.getStartDate3());
                        long endDate1 = TimeUtils.getNowDateAnTime(attendanceTimeDTO.getEndDate1());
                        long endDate2 = TimeUtils.getNowDateAnTime(attendanceTimeDTO.getEndDate2());
                        long endDate3 = TimeUtils.getNowDateAnTime(attendanceTimeDTO.getEndDate3());
                        if (ad.getWeek().equals(TimeUtils.getNowWeek())) {
                            if (startDate1 != 0 && endDate1 != 0) {
                                if (startDate1 < finalCaptureTime && finalCaptureTime < endDate1) {
                                    status[0] = deviceDTO.getInAndOut().equals(ParkVdcType.IN.name()) ? AtStatus.LATE.name() : AtStatus.LACK.name();
                                    num[0] = 1;
                                }
                            }
                            if (startDate2 != 0 && endDate2 != 0) {
                                if (startDate2 < finalCaptureTime && finalCaptureTime < endDate2) {
                                    status[0] = deviceDTO.getInAndOut().equals(ParkVdcType.IN.name()) ? AtStatus.LATE.name() : AtStatus.LACK.name();
                                    num[0] = 2;
                                }
                            }
                            if (startDate3 != 0 && endDate3 != 0) {
                                if (startDate3 < finalCaptureTime && finalCaptureTime < endDate3) {
                                    status[0] = deviceDTO.getInAndOut().equals(ParkVdcType.IN.name()) ? AtStatus.LATE.name() : AtStatus.LACK.name();
                                    num[0] = 3;
                                }
                            }
                        }
                    });
                    distinguish.setAttendanceNum(num[0]);
                    distinguish.setStatus(status[0]);
                }
                //开门类型
                if (deviceDTO.getDoorType().equals(Type.REMOTE.name())) {
                    if (deviceDTO.getDeviceType().equals(DeviceType.JL_CONTROL.name())) {
                        DeviceOpenDoorReqVO deviceOpenDoorReqVO = new DeviceOpenDoorReqVO();
                        deviceOpenDoorReqVO.setDeviceUUID(distinguish.getUuid());
                        attendanceBaseServer.jlInsideExternalService.openDoor(deviceOpenDoorReqVO);
                    } else if (deviceDTO.getDeviceType().equals(DeviceType.HF_CONTROL.name())) {
                        DeviceOpenDoorReqVO deviceOpenDoorReqVO = new DeviceOpenDoorReqVO();
                        deviceOpenDoorReqVO.setDeviceUUID(distinguish.getUuid());
                        deviceOpenDoorReqVO.setPass(deviceDTO.getDevicePass());
                        attendanceBaseServer.hfInsideExternalService.openDoor(deviceOpenDoorReqVO);
                    }
                }
                //主体类型
                if (deviceDTO.getModuleType().equals(ModuleType.SY.name())) {
                    attendanceBaseServer.atService.save(distinguish);
                }
            } else {
                distinguish.setStatus(AtStatus.NORMAL.name());
                //主体类型
                if (deviceDTO.getModuleType().equals(ModuleType.SY.name())) {
                    attendanceBaseServer.atService.save(distinguish);
                }
            }
        }
    }


    public String imgRecord(String img, String fileName) throws IOException {
        InputStream instream;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes1 = decoder.decodeBuffer(img);
        instream = new ByteArrayInputStream(bytes1);
        String type = fileName.substring(fileName.lastIndexOf("."));
        fileName = UuidUtils.generateUuid() + type;
        System.out.println(fileName);
        String key = OssClienUtils.uploadFileOSS(instream, fileName);
        System.out.println(key);
        String url = OssClienUtils.getUrl1("dashu" + fileName);
        System.out.println(url);
        return url;
    }
}
